//debugger;
document.writeln('<iframe id="DateLayer" frameborder="0" style="position: absolute; width:200px; height:280px; z-index: 19998; display:none;"></iframe>');
var calendarFrameText = '';  //存放日历层的HTML代码
calendarFrameText += '<style type="text/css">	';
calendarFrameText += '';
//new
calendarFrameText += ".calendar {";
calendarFrameText += "	width: 180px;";
calendarFrameText += "	border: 1px solid #B7CFEE;";
calendarFrameText += "	background: #FFF;";
calendarFrameText += "	font: 14px Arial, Helvetica, sans-serif;";
calendarFrameText += "}";
calendarFrameText += ".calendar td {";
calendarFrameText += "	color: #333;";
calendarFrameText += "	text-align: center;";
calendarFrameText += "	vertical-align: middle;";
calendarFrameText += "	cursor: pointer;";
calendarFrameText += "}";
calendarFrameText += ".calendar .month {";
calendarFrameText += "	width: 100%;";
calendarFrameText += "	height: 25px;";
calendarFrameText += "	border-bottom: 1px solid #B7CFEE;";
calendarFrameText += "}";
calendarFrameText += ".calendar td.prev, .calendar td.next {";
calendarFrameText += "	width: 25px;";
calendarFrameText += "	border-right: 1px solid #B7CFEE;";
calendarFrameText += "	background: #ECF2FB;";
calendarFrameText += "}";
calendarFrameText += ".calendar td.next {";
calendarFrameText += "	border-left: 1px solid #B7CFEE;";
calendarFrameText += "	border-right: 0;";
calendarFrameText += "}";
calendarFrameText += ".calendar td.prev:hover, .calendar td.next:hover {";
calendarFrameText += "	background: #B7CFEE;";
calendarFrameText += "}";
calendarFrameText += ".calendar .weekWrap {";
calendarFrameText += "	padding: 3px 0;";
calendarFrameText += "	background: #D3E2F5;";
calendarFrameText += "	border-bottom: 1px solid #B7CFEE;";
calendarFrameText += "}";
calendarFrameText += ".calendar .week {";
calendarFrameText += "	margin: 0 auto;";
calendarFrameText += "}";
calendarFrameText += ".calendar .week td {";
calendarFrameText += "	width: 20px;";
calendarFrameText += "	height: 20px;";
calendarFrameText += "	border: 1px solid #93B6E6;";
calendarFrameText += "	background: #B7CFEE;";
calendarFrameText += "	color: #FFF;";
calendarFrameText += "}";
calendarFrameText += ".calendar .dayWrap {";
calendarFrameText += "	padding: 3px 0;";
calendarFrameText += "}";
calendarFrameText += ".calendar .day {";
calendarFrameText += "	margin: 0 auto;";
calendarFrameText += "}";
calendarFrameText += ".calendar .day td {";
calendarFrameText += "	width: 20px;";
calendarFrameText += "	height: 20px;";
calendarFrameText += "	border: 1px solid #B7CFEE;";
calendarFrameText += "	background: #ECF2FB;";
calendarFrameText += "}";
calendarFrameText += ".calendar td.selectDate, .calendar .day td:hover {";
calendarFrameText += "	border: 1px solid #947B05;";
calendarFrameText += "	background: #FBE994;";
calendarFrameText += "}";
calendarFrameText += ".calendar .undefined {";
calendarFrameText += "	color: #999;";
calendarFrameText += "}";
calendarFrameText += ".calendar td.close {";
calendarFrameText += "	width: auto;";
calendarFrameText += "	background: #FFF;";
calendarFrameText += "}";
calendarFrameText += ".calendar .switch {";
calendarFrameText += "	width: 100%;";
calendarFrameText += "	height: 25px;";
calendarFrameText += "	border-top: 1px solid #B7CFEE;";
calendarFrameText += "}";
calendarFrameText += "";
calendarFrameText += '</style>';

calendarFrameText += '<div style="z-index:9999;position: absolute; left:0; top:0;" onselectstart="return false">';
calendarFrameText += '    <span id=tmpSelectYearLayer style="z-index: 9999;position: absolute;top: 3; left: 110;display: none"></span>';
calendarFrameText += '    <span id=tmpSelectMonthLayer style="z-index: 9999;position: absolute;top: 3; left: 19;display: none"></span>';
calendarFrameText += '    ';
calendarFrameText += '    <table cellpadding=\"0\" cellspacing=\"0\" class=\"calendar\">';

//new one row
calendarFrameText += "    <tr>";
calendarFrameText += "      <td><table cellpadding=\"0\" cellspacing=\"0\" class=\"month\">";
calendarFrameText += "          <tbody>";
calendarFrameText += "            <tr>";
calendarFrameText += "              <td title=\"Last Month\" onclick=\"parent.PrevM()\" class=\"prev\">&lt;<\/td>";
calendarFrameText += "              <td onmouseover=\"style.backgroundColor=\'#B7CFEE\'\" title=\"Click to Choose Month\" onmouseout=\"style.backgroundColor=\'#FFF\'\" onclick=\"parent.tmpSelectMonthInnerHTML()\"><span id=\"MonthHead\" month=\"10\">November<\/span><\/td>";
calendarFrameText += "              <td onmouseover=\"style.backgroundColor=\'#B7CFEE\'\" title=\"Click to Choose Year\" onmouseout=\"style.backgroundColor=\'#FFF\'\" onclick=\"parent.tmpSelectYearInnerHTML()\"><span id=\"YearHead\" year=\"2009\">2009<\/span><\/td>";
calendarFrameText += "              <td title=\"Next Month\" onclick=\"parent.NextM()\" class=\"next\">&gt;<\/td>";
calendarFrameText += "            <\/tr>";
calendarFrameText += "          <\/tbody>";
calendarFrameText += "        <\/table><\/td>";
calendarFrameText += "    <\/tr>";
//new two row
calendarFrameText += "    <tr>";
calendarFrameText += "      <td class=\"weekWrap\"><table cellspacing=\"2\" cellpadding=\"0\" class=\"week\">";
calendarFrameText += "          <tbody>";
calendarFrameText += "            <tr>";
calendarFrameText += "              <td>S<\/td>";
calendarFrameText += "              <td>M<\/td>";
calendarFrameText += "              <td>T<\/td>";
calendarFrameText += "              <td>W<\/td>";
calendarFrameText += "              <td>T<\/td>";
calendarFrameText += "              <td>F<\/td>";
calendarFrameText += "              <td>S<\/td>";
calendarFrameText += "            <\/tr>";
calendarFrameText += "          <\/tbody>";
calendarFrameText += "        <\/table><\/td>";
calendarFrameText += "    <\/tr>";
//new three row
calendarFrameText += '    <tr >';
calendarFrameText += '        <td class=dayWrap>';
calendarFrameText += '            <table id="calendarDateTable" cellspacing=2 cellpadding=0 class=day>  ';
for (var i = 0; i <= 38; i++) {
    if (i == 0) {
        calendarFrameText += '                <tr>';
    }
    else if (i % 7 == 0) {
        calendarFrameText += '               </tr> ';
        calendarFrameText += '               <tr>';
    }
    calendarFrameText += '                    <td  id=Day' + i + '>' + i + '</td>';
}
calendarFrameText += "                      <td colSpan=3 class=\"close\"><span onclick=\"parent.DayClick(null,null,true);\"><ins>Clear<\/ins><\/span><\/td>";
calendarFrameText += '               </tr>    ';
calendarFrameText += '           </table>';
calendarFrameText += '        </td>';
calendarFrameText += '    </tr>';
//new four row
calendarFrameText += "<tr>";
calendarFrameText += "      <td><table cellSpacing=\"0\" cellPadding=\"0\" class=\"switch\">";
calendarFrameText += "          <tbody>";
calendarFrameText += "            <tr>";
calendarFrameText += "              <td class=\"prev\"><div class=\"calendarButton\" onfocus=\"this.blur()\" title=\"Last Year\" onclick=\"parent.PrevY()\">&lt;&lt;<\/div><\/td>";
calendarFrameText += "              <td class=\"prev\"><div class=\"calendarButton\" onfocus=\"this.blur()\" title=\"Last Month\" onclick=\"parent.PrevM()\">&lt;<\/div><\/td>";
calendarFrameText += "              <td><div class=\"calendarButton\" onfocus=\"this.blur()\" title=\"Current Date\" onclick=\"parent.Today()\">Today<\/div><\/td>";
calendarFrameText += "              <td class=\"next\"><div class=\"calendarButton\" onfocus=\"this.blur()\" title=\"Next Month\" onclick=\"parent.NextM()\")>&gt;<\/div><\/td>";
calendarFrameText += "              <td class=\"next\"><div class=\"calendarButton\" onfocus=\"this.blur()\" title=\"Next Year\" onclick=\"parent.NextY()\">&gt;&gt;<\/div><\/td>";
calendarFrameText += "            <\/tr>";
calendarFrameText += "          <\/tbody>";
calendarFrameText += "        <\/table><\/td>";
calendarFrameText += "    <\/tr>";

calendarFrameText += '</table>';
calendarFrameText += '</div>';

if (isIE()) {
    document.frames["DateLayer"].document.writeln(calendarFrameText);
    document.frames["DateLayer"].document.close();  //解决ie进度条不结束的问题

    document.getElementById("DateLayer").style.width = "180px";
    document.getElementById("DateLayer").style.height = "223px";
}
else {
    var obj = document.getElementById("DateLayer").contentDocument;
    obj.writeln(calendarFrameText);
    obj.close();
}


//==================================================== WEB 页面显示部分 ======================================================
var outObject;
var outDate = "";  //存放对象的日期
var odatelayer;  //存放日历对象
if (isIE()) {
    odatelayer = window.frames["DateLayer"].document;
}
else {
    odatelayer = document.getElementById("DateLayer").contentDocument;
}

function setday(obj, date) //主调函数
{
    if (arguments.length > 2) { MsgBox("Too many parameter!"); return; }
    if (arguments.length < 1) { MsgBox("No enough parameter!"); return; }
    var dads = document.getElementById("DateLayer").style;

    outObject = obj;
    var ttop = obj.offsetTop;     //控件的定位点高
    var thei = obj.clientHeight;  //控件本身的高
    var tleft = obj.offsetLeft;    //控件的定位点宽
    var ttyp = obj.type;          //控件的类型
    while (obj = obj.offsetParent) { ttop += obj.offsetTop; tleft += obj.offsetLeft; }
    dads.top = ((ttyp == "image") ? ttop + thei : ttop + thei + 6) + "px";
    dads.left = tleft + "px";

    dads.display = '';
    if (date == undefined || date == "") {
        outDate = new Date();
    }
    else {
        outDate = date;
    }
    SetDay(outDate.getFullYear(), outDate.getMonth() + 1);

}

var MonDayCount = new Array(12);         //定义阳历中每个月的最大天数
MonDayCount[0] = 31; MonDayCount[1] = 28; MonDayCount[2] = 31; MonDayCount[3] = 30; MonDayCount[4] = 31; MonDayCount[5] = 30;
MonDayCount[6] = 31; MonDayCount[7] = 31; MonDayCount[8] = 30; MonDayCount[9] = 31; MonDayCount[10] = 30; MonDayCount[11] = 31;

var MonName = new Array(12);        //定义月的名称
MonName[1] = "January"; MonName[2] = "February"; MonName[3] = "March"; MonName[4] = "April"; MonName[5] = "May"; MonName[6] = "June";
MonName[7] = "July"; MonName[8] = "August"; MonName[9] = "September"; MonName[10] = "October"; MonName[11] = "November"; MonName[12] = "December";

var TheYear = new Date().getFullYear(); //定义年的变量的初始值
var TheMonth = new Date().getMonth() + 1; //定义月的变量的初始值
var WDay = new Array(39);               //定义写日期的数组

//任意点击时关闭该控件 
document.onclick = function(e) {
    var theEvent = window.event || e;
    var srcElement = theEvent.srcElement;
    if (!srcElement) {
        srcElement = theEvent.target;
    }
    if (srcElement != outObject && typeof (srcElement.attributes) != "undefined" && srcElement.attributes["CalendarIcon"] == null
        && srcElement.attributes["calendaricon"] == null) {
        closeLayer();
    }
}

//按Esc键关闭，切换焦点关闭
document.onkeydown = function(e) {
    var theEvent = window.event || e;
    var code = theEvent.keyCode || theEvent.which;
    if (code == 27) {
        if (outObject) {
            outObject.blur();
        }
        closeLayer();
    }
    else if (document.activeElement) {
        if (document.activeElement != outObject) {
            closeLayer();
        }
    }
}

//往 head 中写入当前的年与月
function WriteHead(yy, mm) {
    mm = (mm - 1) % 12 + 1;
    var YearHead = odatelayer.getElementById("YearHead");
    var MonthHead = odatelayer.getElementById("MonthHead");
    YearHead.year = yy;
    MonthHead.month = mm;
    if (isIE()) {
        YearHead.innerText = yy;
        MonthHead.innerText = MonName[mm];
    }
    else {
        YearHead.textContent = yy;
        MonthHead.textContent = MonName[mm];
    }
}

//年份的下拉框
function tmpSelectYearInnerHTML() {
    var m = odatelayer.getElementById("YearHead").year;
    if (m < 1000 || m > 9999) {
        MsgBox("Year Range:(1000-9999)");
        return;
    }
    var n = m - 10;
    if (n < 1000) {
        n = 1000;
    }
    if (n + 20 > 9999) {
        n = 9999 - 20;
    }
    var s = "<select id=tmpSelectYear style='font-size: 12px' "
    s += "onblur='document.getElementById(\"tmpSelectYearLayer\").style.display=\"none\"' "
    s += "onchange='document.getElementById(\"tmpSelectYearLayer\").style.display=\"none\";"
    s += "parent.TheYear = this.value; parent.SetDay(parent.TheYear,parent.TheMonth)'>\r\n";
    var selectInnerHTML = s;
    for (var i = n; i < n + 20; i++) {
        if (i == m) {
            selectInnerHTML += "<option value='" + i + "' selected>" + i + "</option>\r\n";
        }
        else {
            selectInnerHTML += "<option value='" + i + "'>" + i + "</option>\r\n";
        }
    }
    selectInnerHTML += "</select>";
    var tmpSelectYearLayer = odatelayer.getElementById("tmpSelectYearLayer");
    tmpSelectYearLayer.style.display = "";
    tmpSelectYearLayer.innerHTML = selectInnerHTML;
    odatelayer.getElementById("tmpSelectYear").focus();
}

//月份的下拉框
function tmpSelectMonthInnerHTML() {
    var MonthHead = odatelayer.getElementById("MonthHead");
    var m = MonthHead.month;
    var s = "<select id=tmpSelectMonth style='font-size: 12px' "
    s += "onblur='document.getElementById(\"tmpSelectMonthLayer\").style.display=\"none\"' "
    s += "onchange='document.getElementById(\"tmpSelectMonthLayer\").style.display=\"none\";"
    s += "parent.TheMonth = Number(this.value); parent.SetDay(parent.TheYear,parent.TheMonth)'>\r\n";
    var selectInnerHTML = s;
    for (var i = 1; i <= 12; i++) {
        if (i == m) {
            selectInnerHTML += "<option value='" + i + "' selected>" + MonName[i] + "</option>\r\n";
        }
        else {
            selectInnerHTML += "<option value='" + i + "'>" + MonName[i] + "</option>\r\n";
        }
    }
    selectInnerHTML += "</select>";
    var tmpSelectMonthLayer = odatelayer.getElementById("tmpSelectMonthLayer");
    tmpSelectMonthLayer.style.display = "";
    tmpSelectMonthLayer.innerHTML = selectInnerHTML;
    odatelayer.getElementById("tmpSelectMonth").focus();
}

//这个层的关闭
function closeLayer() {
    document.getElementById("DateLayer").style.display = "none";
}

//判断是否闰平年
function IsPinYear(year) {
    if (0 == year % 4 && ((year % 100 != 0) || (year % 400 == 0))) {
        return true;
    }
    else {
        return false;
    }
}

//闰年二月为29天
function GetMonthCount(year, month) {
    var c = MonDayCount[month - 1];
    if ((month == 2) && IsPinYear(year)) {
        c++;
    }
    return c;
}

function PrevY()  //往前翻 Year
{
    if (TheYear > 999 && TheYear < 10000) {
        TheYear--;
    }
    else {
        MsgBox("Year Range:(1000-9999)");
    }
    SetDay(TheYear, TheMonth);
}
function NextY()  //往后翻 Year
{
    if (TheYear > 999 && TheYear < 10000) {
        TheYear++;
    }
    else {
        MsgBox("Year Range:(1000-9999)");
    }
    SetDay(TheYear, TheMonth);
}
function Today()  //Today Button
{
    outObject.NewDate = new Date();
    eval(outObject.attributes["DeelSelectDate"].value + "(outObject)");
    closeLayer();
}
function PrevM()  //往前翻月份
{
    if (TheMonth > 1) {
        TheMonth--;
    }
    else {
        TheYear--;
        TheMonth = 12;
    }
    SetDay(TheYear, TheMonth);
}
function NextM()  //往后翻月份
{
    if (TheMonth == 12) {
        TheYear++;
        TheMonth = 1;
    }
    else {
        TheMonth++;
    }
    SetDay(TheYear, TheMonth);
}

function SetDay(yy, mm)   //主要的写程序**********
{
    //debugger;
    WriteHead(yy, mm);
    //设置当前年月的公共变量为传入值
    TheYear = yy;
    TheMonth = mm;

    //将显示框的内容全部清空
    for (var i = 0; i < 39; i++) {
        WDay[i] = ""
    };
    var day1 = 1, day2 = 1, firstday = new Date(yy, mm - 1, 1).getDay();  //某月第一天的星期几

    for (i = 0; i < firstday; i++) {
        WDay[i] = GetMonthCount(mm == 1 ? yy - 1 : yy, mm == 1 ? 12 : mm - 1) - firstday + i + 1; //上个月的最后几天
    }
    for (i = firstday; day1 < GetMonthCount(yy, mm) + 1; i++) {
        WDay[i] = day1;
        day1++;
    }
    for (i = firstday + GetMonthCount(yy, mm); i < 39; i++) {
        WDay[i] = day2;
        day2++;
    }
    for (i = 0; i < 39; i++) {
        var da = odatelayer.getElementById("Day" + i);   //书写新的一个月的日期星期排列
        da.className = "";
        da.day = WDay[i];
        if (WDay[i] != "") {
            //初始化边框

            if (i < firstday)  //上个月的部分
            {
                //da.innerHTML = "<b><font color=gray>" + WDay[i] + "</font></b>";
                da.innerHTML = "" + WDay[i] + "";
                da.className = "undefined";
                da.title = MonName[(mm == 1 ? 12 : mm - 1)] + " " + WDay[i];
                da.onclick = Function("DayClick(this.day,-1)");
                if (!outDate) {
                    //如果是今天，显示不同的背景
                    da.style.backgroundColor = ((mm == 1 ? yy - 1 : yy) == new Date().getFullYear() &&
                        (mm == 1 ? 12 : mm - 1) == new Date().getMonth() + 1 && WDay[i] == new Date().getDate()) ?
                         "#FFD700" : "#e0e0e0";
                }
                else {
                    //                    da.style.backgroundColor = ((mm == 1 ? yy - 1 : yy) == outDate.getFullYear() && (mm == 1 ? 12 : mm - 1) == outDate.getMonth() + 1 &&
                    //                        WDay[i] == outDate.getDate()) ? "#00ffff" :
                    //                        (((mm == 1 ? yy - 1 : yy) == new Date().getFullYear() && (mm == 1 ? 12 : mm - 1) == new Date().getMonth() + 1 &&
                    //                        WDay[i] == new Date().getDate()) ? "#FFD700" : "#e0e0e0");

                    //将选中的日期显示为凹下去
                    if ((mm == 1 ? yy - 1 : yy) == outDate.getFullYear() && (mm == 1 ? 12 : mm - 1) == outDate.getMonth() + 1 &&
                        WDay[i] == outDate.getDate()) {
                        da.className = "selectDate";
                    }
                }
            }
            else if (i >= firstday + GetMonthCount(yy, mm))  //下个月的部分
            {
                da.innerHTML = "" + WDay[i] + "";
                da.className = "undefined";
                da.title = MonName[(mm == 12 ? 1 : mm + 1)] + " " + WDay[i];
                da.onclick = Function("DayClick(this.day,1)");
                if (!outDate) {
                    da.style.backgroundColor = ((mm == 12 ? yy + 1 : yy) == new Date().getFullYear() &&
                        (mm == 12 ? 1 : mm + 1) == new Date().getMonth() + 1 && WDay[i] == new Date().getDate()) ?
                        "#FFD700" : "#e0e0e0";
                }
                else {
                    //                    da.style.backgroundColor = ((mm == 12 ? yy + 1 : yy) == outDate.getFullYear() && (mm == 12 ? 1 : mm + 1) == outDate.getMonth() + 1 &&
                    //                    WDay[i] == outDate.getDate()) ? "#00ffff" :
                    //                        (((mm == 12 ? yy + 1 : yy) == new Date().getFullYear() && (mm == 12 ? 1 : mm + 1) == new Date().getMonth() + 1 &&
                    //                    WDay[i] == new Date().getDate()) ? "#FFD700" : "#e0e0e0");
                    //将选中的日期显示为凹下去
                    if ((mm == 12 ? yy + 1 : yy) == outDate.getFullYear() && (mm == 12 ? 1 : mm + 1) == outDate.getMonth() + 1 &&
                        WDay[i] == outDate.getDate()) {
                        da.className = "selectDate";
                    }
                }
            }
            else  //本月的部分
            {
                da.innerHTML = "" + WDay[i] + "";
                da.title = MonName[mm] + " " + WDay[i];
                da.onclick = Function("DayClick(this.day,0)");  //给td赋予onclick事件的处理
                //如果是当前选择的日期，则显示亮蓝色的背景；如果是当前日期，则显示暗黄色背景
                if (!outDate) {
                    da.style.backgroundColor = (yy == new Date().getFullYear() && mm == new Date().getMonth() + 1 && WDay[i] == new Date().getDate()) ?
                        "#FFD700" : "#e0e0e0";
                }
                else {
                    //                    da.style.backgroundColor = (yy == outDate.getFullYear() && mm == outDate.getMonth() + 1 && WDay[i] == outDate.getDate()) ?
                    //                        "#00ffff" : ((yy == new Date().getFullYear() && mm == new Date().getMonth() + 1 && WDay[i] == new Date().getDate()) ?
                    //                        "#FFD700" : "#e0e0e0");
                    //将选中的日期显示为凹下去
                    if (yy == outDate.getFullYear() && mm == outDate.getMonth() + 1 && WDay[i] == outDate.getDate()) {
                        da.className = "selectDate";
                    }
                }
            }
        }
        else {

        }
    }
}

function DayClick(n, ex, clear)  //点击显示框选取日期，主输入函数*************
{
    if ( clear==undefined || !clear) {
        var yy = TheYear;
        var mm = parseInt(TheMonth) + ex; //ex表示偏移量，用于选择上个月份和下个月份的日期
        //判断月份，并进行对应的处理
        if (mm < 1) {
            yy--;
            mm = 12 + mm;
        }
        else if (mm > 12) {
            yy++;
            mm = mm - 12;
        }

        outObject.NewDate = new Date(yy, mm - 1, eval(n));
    }
    else {
        outObject.NewDate = "";
    }

    var DeelSelectDate = outObject.DeelSelectDate || outObject.attributes["DeelSelectDate"].value;
    eval(DeelSelectDate + "(outObject)");
    closeLayer();
}

function SelectDateText(obj) {
    obj.value = DateToStringUS(obj.NewDate);
}